package com.spark.lola.controller.logistics;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.logistics.constant.LogisticsConstant;
import com.spark.lola.logistics.entity.CarInfo;
import com.spark.lola.logistics.mapper.CarInfoMapper;
import com.spark.lola.logistics.mapper.DriverInfoMapper;
import com.spark.lola.logistics.service.CarInfoService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.DataBeanUtils;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.controller.BaseController;
import com.spark.lola.support.mybatis.entity.QueryParam;
import com.spark.lola.support.wms.cache.CollectionCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Zhi-peng wang
 * @date: 2020/04/24 13:56
 * 车辆信息接口
 **/
@RestController
@RequestMapping("/api/logistics/car")
@Api(value = "/api/logistics/car", tags = "车辆信息接口")
public class CarInfoController extends BaseController {

    @Autowired
    private CarInfoService carInfoService;

    @Autowired
    private DriverInfoMapper driverInfoMapper;

    @Autowired
    private CarInfoMapper carInfoMapper;

    @ApiOperation(value = "分页查询车辆信息")
    @RequiresPermissions(value = "m5110")
    @RequestMapping(value = "/findBy", method = RequestMethod.POST)
    public ResponseBean findPage(@RequestBody QueryParam<CarInfo> queryParam) {
        IPage<CarInfo> carInfoVoIPage = carInfoService.findPage(queryParam);
        return ResponseBean.success(carInfoVoIPage);
    }

    @RequestMapping("/existNo")
    public ResponseBean existNo(@RequestParam String no, @RequestParam(value = "id", defaultValue = "0") Long id) {
        int count = carInfoMapper.existNo(id, no);
        return ResponseBean.success(null, count);
    }

    @ApiOperation(value = "获取车辆列表")
    @ApiImplicitParam(name = "key", value = "模糊查询字段，查询车牌", required = true, dataType = "String", paramType = "query")
    @RequestMapping(value = "/map/{isBind}")
    public ResponseBean map(@RequestParam String key, @PathVariable int isBind) {
        return ResponseBean.success(carInfoService.selectMap(key, isBind));
    }

    @ApiOperation(value = "获取车辆信息详情")
    @ApiImplicitParam(name = "id", value = "车辆信息id", required = true, dataType = "long", paramType = "path")
    @RequiresPermissions(value = "m5114")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.GET)
    public ResponseBean findInfo(@PathVariable long id, @SessionAttribute ShiroUser shiroUser) {
        CarInfo carInfo = new CarInfo();
        if (id != 0) {
            carInfo = carInfoService.getById(id);
            if (carInfo.getIsBind() == LogisticsConstant.CarIsBind.Y_BIND) {
                long empId = driverInfoMapper.findEmpIdByCarId(carInfo.getId());
                carInfo.setDriverInfo(CollectionCache.employees.get(empId));
            }
        }
        return ResponseBean.success(carInfo);
    }

    @ApiOperation(value = "添加/修改车辆信息")
    @RequiresPermissions(value = {"m5111", "m5113"}, logical = Logical.OR)
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseBean save(@RequestBody CarInfo carInfo, @SessionAttribute ShiroUser shiroUser) {
        if (carInfoMapper.countNo(carInfo.getNo(), carInfo.getId()) != 0) {
            return ResponseBean.error("车牌号已存在！");
        }
        CarInfo model = new CarInfo();
        if (carInfo.getId() != null) {
            model = carInfoService.getById(carInfo.getId());
            // 非修改项置空
            model.setIsBind(null);
            model.setUsed(null);
            // 系统修改项赋值
        } else {
            // 设置初始化项
        }
        DataBeanUtils.buildUpdateBean(model, carInfo);
        carInfoService.saveOrUpdate(model);
        return ResponseBean.success(model);
    }

    @ApiOperation(value = "启用车辆")
    @ApiImplicitParam(name = "nos", value = "车辆信息id字符串集", required = true, dataType = "String", paramType = "query")
    @RequiresPermissions(value = "m5115")
    @RequestMapping(value = "/on", method = RequestMethod.GET)
    public ResponseBean on(@RequestParam String nos) {
        if (StringUtils.isBlank(nos)) {
            return ResponseBean.error("字符串集不能为空");
        }
        int count = carInfoService.updateUsedByIds(CommonConstant.Y, nos);
        return ResponseBean.success(null, count);
    }

    @ApiOperation(value = "禁用车辆")
    @ApiImplicitParam(name = "nos", value = "车辆信息id字符串集", required = true, dataType = "String", paramType = "query")
    @RequiresPermissions(value = "m5116")
    @RequestMapping(value = "/un", method = RequestMethod.GET)
    public ResponseBean un(@RequestParam String nos) {
        if (StringUtils.isBlank(nos)) {
            return ResponseBean.error("id字符串集不能为空");
        }
        int count = carInfoService.updateUsedByIds(CommonConstant.N, nos);
        return ResponseBean.success(null, count);
    }

    @ApiOperation(value = "删除车辆信息")
    @ApiImplicitParam(name = "nos", value = "车辆信息id集合", required = true, dataType = "Long", paramType = "query")
    @RequiresPermissions(value = "m5112")
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public ResponseBean del(@RequestParam(value = "nos") List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return ResponseBean.error("id字符串集不能为空");
        }
        if (ids.size() > 1) {
            return ResponseBean.error("禁止批量操作");
        }
        int isBind = carInfoService.findIsBind(ids.get(0));
        if (isBind == LogisticsConstant.CarIsBind.Y_BIND) {
            return ResponseBean.error("该车辆已绑定驾驶员，请解绑后操作");
        }
        carInfoService.removeById(ids.get(0));
        return ResponseBean.success();
    }

}
